package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.SysUserCommonFunDTO;
import com.xinsoft.entity.dto.SysUserReportDTO;
import com.xinsoft.entity.po.SysUserCommonFun;
import com.xinsoft.entity.po.SysUserReport;
import com.xinsoft.mapper.SysUserReportMapper;
import com.xinsoft.service.SysMenuService;
import com.xinsoft.service.SysReportConfigService;
import com.xinsoft.service.SysUserReportService;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author hsm
* @description 针对表【sys_user_report(用户常用报表)】的数据库操作Service实现
* @createDate 2023-11-21 16:51:24
*/
@Service
public class SysUserReportServiceImpl extends ServiceImpl<SysUserReportMapper, SysUserReport>
    implements SysUserReportService {


    @Resource
    private SysReportConfigService sysReportConfigService;

    @Override
    public List<SysUserReportDTO> findList() {
        QueryWrapper query = new QueryWrapper<SysUserCommonFun>().eq("user_id", TokenUtil.getUserId()).orderByAsc("sort");
        List<SysUserReport> list = list(query);
        List<SysUserReportDTO> result = new ArrayList<>();

        for (SysUserReport sysUserReport : list) {
            SysUserReportDTO record = new SysUserReportDTO();
            BeanUtils.copyProperties(sysUserReport,record);
            record.setSysReportConfig(sysReportConfigService.getById(sysUserReport.getReportId()));
            result.add(record);
        }
        return result;
    }

    @Override
    public void edit(List<SysUserReport> sysUserReportList) {
        //先删除原有设置
        QueryWrapper removeQuery = new QueryWrapper<SysUserCommonFun>().eq("user_id", TokenUtil.getUserId());
        remove(removeQuery);
        if(CollectionUtil.isNotEmpty(sysUserReportList)){
            for (SysUserReport sysUserReport : sysUserReportList) {
                sysUserReport.setUserId(TokenUtil.getUserId());
            }
        }
        saveBatch(sysUserReportList);
        //再保存最新的设置
    }
}




